import {createI18n} from 'vue-i18n';
import pinia from '/@/stores/index';
import {storeToRefs} from 'pinia';
import {useThemeConfig} from '/@/stores/themeConfig';
import zhCNLocale from 'element-plus/es/locale/lang/zh-cn';
import zhTWLocale from 'element-plus/es/locale/lang/zh-tw';
import enLocale from 'element-plus/es/locale/lang/en';

import nextZhCN from '/@/i18n/lang/zh-cn';
import nextEn from '/@/i18n/lang/en';
import nextZhTW from '/@/i18n/lang/zh-tw';

import pagesLoginZhCN from '/@/i18n/pages/login/zh-cn';
import pagesLoginEn from '/@/i18n/pages/login/en';
import pagesLoginZhTW from '/@/i18n/pages/login/zh-tw';
import pagesFormI18nZhCN from '/@/i18n/pages/formI18n/zh-cn';
import pagesFormI18nEn from '/@/i18n/pages/formI18n/en';
import pagesFormI18nZhTW from '/@/i18n/pages/formI18n/zh-tw';

// 定义语言国际化内容
/**
 * 说明：
 * /src/i18n/lang 下的 ts 为框架的国际化内容
 * /src/i18n/pages 下的 ts 为各界面的国际化内容
 */
const messages = {
    [zhCNLocale.name]: {
        ...zhCNLocale,
        message: {
            ...nextZhCN,
            ...pagesLoginZhCN,
            ...pagesFormI18nZhCN,
        },
    },
    [enLocale.name]: {
        ...enLocale,
        message: {
            ...nextEn,
            ...pagesLoginEn,
            ...pagesFormI18nEn,
        },
    },
    [zhTWLocale.name]: {
        ...zhTWLocale,
        message: {
            ...nextZhTW,
            ...pagesLoginZhTW,
            ...pagesFormI18nZhTW,
        },
    },
};

// 读取 pinia 默认语言
const stores = useThemeConfig(pinia);
const {themeConfig} = storeToRefs(stores);

// 导出语言国际化
// https://vue-i18n.intlify.dev/guide/essentials/fallback.html#explicit-fallback-with-one-locale
export const i18n = createI18n({
    silentTranslationWarn: true,
    missingWarn: false,
    silentFallbackWarn: true,
    fallbackWarn: false,
    locale: themeConfig.value.globalI18n,
    fallbackLocale: zhCNLocale.name,
    legacy: false,
    messages,
});
